/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet FRAME
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
 Name:               frame name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 width:              Width 
 height              height
 title				 title of the frame
 open				 initial open status (true if open)
 dynamic			 if true, the frame is able to open and close
 position			 style position 'absolute' or 'relative'
 draggable			 true if the frame is draggable
 parent              object parent  		        
*************************************************************************************************/


// *************************************
// CONSTRUCTOR
// *************************************

//todo : 
//  version n+1 revise the minimum widt hlimit : today 253 px !!!!!!!!!!!!!!!!
//	version n+1	revise the design of title :
//				 		replace innerHtml
//						replace IMG with DIV : export images in css or div3D.js ...
//	version n+2	merge Frame and Window ?

rialto.widget.Frame=function(objPar){
    //base constructor
    this.base = rialto.widget.AbstractContainer;
    objPar.type="frame";
    this.base(objPar);
         
    //Default value
    this.title = "Frame";  
    this.printTitle=this.title;     
    this.dynamic = false;
    this.open = true;
    this.boolPrint = false; 
    this.boolMaxi = false;
    this.autoResizeContenu = false;
    this.autoResizeParent =false;
    this.autoResizableH=false;  
    this.autoResizableW=false;  

   
    //set the value with the properties of the objPar object    
    if (rialto.lang.isBoolean(objPar.dynamic)){
        this.dynamic = objPar.dynamic;        
    }
    if (rialto.lang.isBoolean(objPar.open)){
        this.open = objPar.open;        
    }
    if (rialto.lang.isString(objPar.title)){
        this.title = objPar.title; 
        this.titrePrint=this.title;             
    }
    if (rialto.lang.isString(objPar.printTitle)){
        this.printTitle = objPar.printTitle;        
    }    
    if (rialto.lang.isBoolean(objPar.autoResizeContenu)){
        this.autoResizeContenu = objPar.autoResizeContenu;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizeParent)){        
        this.autoResizeParent = objPar.autoResizeParent;        
        this.autoResizableH=this.autoResizeParent;
    }                            
    if (rialto.lang.isBoolean(objPar.boolPrint)){
        this.boolPrint = objPar.boolPrint;        
    }
    if (rialto.lang.isBoolean(objPar.boolMaxi)){
        this.boolMaxi = objPar.boolMaxi;        
    }


    var oThis=this;    
    
    // *******************************************
	// GENERATION OF DOM ELEMENTS
	// *******************************************
	
    this.divExt.style.top = this.top;
    this.divExt.style.left = this.left;
    this.divExt.style.width = this.width;        
    this.divExt.style.height = this.height;        
    this.divExt.style.position=this.position;         
    this.divExt.style.backgroundColor="#F3F3F3";
    this.divExt.id = this.name;            
    //FRAME
    this.cadre = document.createElement("DIV");
    this.cadre.id = this.id;
    this.cadre.name = this.name;                
    this.cadre.style.position="relative";
    this.cadre.className = "div-cadre"; 
    this.cadre.style.width = '100%';     
    this.divExt.appendChild(this.cadre);                                           
    if (objPar.parent) {this.placeIn(objPar.parent);}; 	

	//release object
	objPar=null;
}

rialto.widget.Frame.prototype = new rialto.widget.AbstractContainer;


// ***********************************************************
// UPDATE ELEMENTS AFTER DOM INSERTION
// ***********************************************************

rialto.widget.Frame.prototype.adaptToContext=function() { 

        
        //INITIAL HEIGHT AND WIDTH
	    if (this.autoResizableH){
		    //this.cadre.style.border="1px solid green";
	        this.divExt.style.height=this.getNewParentHeight()-this.divExt.offsetTop;
	    }
	    else{
	        this.divExt.style.height=this.height;
	    }
	    if (this.autoResizableW){
	        this.divExt.style.width=this.getNewParentWidth()-this.divExt.offsetLeft;
	    }
	    else{
	        this.divExt.style.width= this.width;
	    }
		/*traceExec("***********",1);    
    	traceExec("this.title:"+this.title+" this.height:"+this.height+" this.width:"+this.width,1);
    	traceExec("this.divExt.style.width:"+this.divExt.style.width+" this.divExt.style.height:"+this.divExt.style.height,1);
    	*/
        //initial size     
        /*if (this.autoResizeParent){
            //if autoresize keep a min size of 100px
            this.divExt.style.height=Math.max(100,this.getNewParentHeight()-this.divExt.offsetTop);
        }
        else{        
            this.divExt.style.height=this.height; 
        } */       
        if (this.dynamic == true) {                                            	
 	        //this.cadre.style.height = this.getNewParentHeight()-this.cadre.offsetTop
 	        this.cadre.style.height =this.divExt.offsetHeight-this.cadre.offsetTop;
 		}
        else{
            this.cadre.style.height='100%';        
        }    
        this.oldHeight=this.divExt.offsetHeight;
        this.oldWidth=this.divExt.offsetWidth; 
        
     
        var oThis=this;
        /********************DYNAMIC OR NOT ****************************/        
        
        if (this.dynamic) {                                 
        	this.cadre.style.top = 25;
        	//Creation of the toolbar

           	this.toolBar=new rialto.widget.ToolBar({width:"100%",parent:this.divExt});                                    
			//dbleclik
	        this.toolBar.ondbleclick=function(){
		        oThis.setDisplay(!oThis.open);
        	}
			//label for the title
            this.labelTitle=new rialto.widget.Label("",3,10,this.toolBar,this.title,"div-titlecbig");
            

			//Add the differents button
			ob={position:'relative',boolFloatRight:true}
           
            //button +/-
            if (this.open == false) {
                this.cadre.style.display = "none";
                this.divExt.style.height=25;                
                this.BTN = new rialto.widget.Image("moreButtonOff",0,2,this.toolBar,"Open the frame","moreButtonOn",ob);
            }
            else 
            {
                this.cadre.style.display = "block";                                                  
                this.BTN = new rialto.widget.Image("lessButtonOff",0,2,this.toolBar,"Close the frame","lessButtonOn",ob);
            }            
            this.BTN.onclick=function () {
                oThis.setDisplay(!oThis.open);
            };
            
            //button MAXIMIZE
            if(this.boolMaxi){
                this.bMAX = new rialto.widget.Image("maxButtonOff",-5,2,this.toolBar,"Maximise/minimise the frame","maxButtonOn",ob);                                
                this.max = false;
                this.bMAX.onclick=function () {                    
                    oThis.maximize();
                }; 
            }            
            //button PRINT
            if(this.boolPrint){                
                this.bPrint = new rialto.widget.Image("printButtonOff",-10,2,this.toolBar,"Print the content","printButtonOn",ob);                
                this.bPrint.onclick=function () {
                    oThis.print();
                }; 
            }           
        }
        else{
        	//simple frame        	
        	this.cadre.style.border="1px outset white";   
	        //For mozilla to have the top/left border in gray (white by default)
	        if(!window.event){
	            this.cadre.style.borderTopColor="gray";
	            this.cadre.style.borderLeftColor="gray";
	        }   
        	//label for the title
            this.labelTitle=new rialto.widget.Label("",-8,5,this.divExt,this.title,"div-titlecsmall",{position:'absolute'});
        
        }       
}

rialto.widget.Frame.prototype.release=function() {   
	if (this.dynamic) {
		this.BTN.onclick=null;
		if (this.boolMaxi){
			this.boolMaxi.onclick=null;
		}
		if (this.boolPrint){
			this.boolPrint.onclick=null;
		}		
	    //this.toolBar.remove();
	    this.toolBar.ondbleclick=null;
	}
}

    
//function return the html element on which we attach the drag and drop behavior
rialto.widget.Frame.prototype.getHtmlDD=function() {   
    if(this.dynamic){return this.toolBar.divExt}
    else{return this.divExt;}
}

//function return the html element on which we attach the resize behavior
rialto.widget.Frame.prototype.getHtmlRZ=function() {
    //fonction qui renvoie l'element HTML utilis? pour le RZ
    return this.cadre;
}

//function return the html element that contain the element to print
rialto.widget.Frame.prototype.getHtmlImp=function(){    
    return this.cadre;
}

//function return the html element container of the frame
rialto.widget.Frame.prototype.getHtmlCont=function(){    
    return this.cadre;
}

//add a button or image in the title area
rialto.widget.Frame.prototype.addButton=function(objImage) {    
    objImage.getHtmlExt().style.position='relative';
    //objImage.ZoneImage.className='imgOng';
    //on decal ls autres boutons        
    objImage.placeIn(this.DivBTN);                            
            
}

//to print the content ....> kneed to be see again
rialto.widget.Frame.prototype.print=function() {    
        
    //on cherche le fils a imprimer
    if (this.cadre.childNodes.length==1){
        if(this.cadre.childNodes[0].oCiu){
            if(this.cadre.childNodes[0].oCiu.print){
                this.filsAImprimer=this.cadre.childNodes[0].oCiu;                
            }    
        }    
    }
    //si on a un fils on lance sa m?thode print sinon on va appeler la fenetre print et la remplir avec le inner du cadre
    if (this.filsAImprimer){        
        this.filsAImprimer.printTitle=this.printTitle;
        this.filsAImprimer.print();
    }
    else{
      /* 
      //affiche son contenu
        var url="print_elmt?FENID="+this.id+"&NBLIGNE=&NBCOLONNES=&ENTETE=&TAILLECOL=";
        this.fenImp=window.open(url,'fenPrint',"width="+widthGlobal+",height="+heightGlobal+",top=0,left=0,menubar=yes,toolbar=yes");       
        //on va remplir la page avec les data ? imprimer
        obj=this.getZoneImp();
        this.inner=obj.innerHTML;
        this.rempFenImp();
     */
        var widthGlobal=document.body.clientWidth;
        var heightGlobal=document.body.clientHeight;
        this.fenImp=window.open(rialtoConfig.pathRialtoE+'printTab.html','IMPRESSION',"height="+heightGlobal+",width="+widthGlobal+",top=0,left=0,scrollbars,resizable,toolbar,menubar");       
        //get the inner of the frame to print           
        var obj=this.getHtmlImp();
        this.inner=obj.innerHTML;
        this.rempFenImp();        
   }
}

rialto.widget.Frame.prototype.rempFenImp = function(){                   
    var obj={titre:this.printTitle,divInner:this.inner};
    //check if the print window have load the function we kneed
    if(this.fenImp.rempPage){
    	this.fenImp.rempPage(obj);
    }
    //if not wait a litte time
    else{
    	window.setTimeout("rialto.session.objects[\""+this.id+"\"].rempFenImp()",50);
   	}
}



//to maximize the frame ...... kneed to be see again (problem with relative position)
rialto.widget.Frame.prototype.maximize=function() {
    //open the fram if it is close
    if(!this.open){    
        this.setDisplay(true);
    }
    //change the image and change the position
    if (this.max==false){    
        this.bMAX.setImageReference("minButtonOff","minButtonOn")
        this.BTN.setVisible(false);         
        this.divExt.style.top = 0;                
        this.divExt.style.left = 0;                
        this.divExt.style.width = '100%';                
        this.divExt.style.height = '100%';                
        this.divExt.style.position='absolute';     
        var delta=this.parent.offsetHeight-this.divExt.offsetHeight-10;        
    }    
    else{        
        this.bMAX.setImageReference("maxButtonOff","maxButtonOn")
        this.BTN.setVisible(true);      
        this.divExt.style.top = this.top;        
        this.divExt.style.left = this.left;               
        this.divExt.style.width = this.width;    
        this.divExt.style.height=this.height;            
        this.divExt.style.position=this.position;
    }
    this.max=!this.max;
    //on retaille le tout
    this.updateSize(delta);    
}

rialto.widget.Frame.prototype.updateSize= function(){       
	this.divExt.style.overflow='hidden';
    this.cadre.style.overflow='hidden';
    if (this.autoResizableH){            
        this.updateHeight();        
    }   
    if(this.autoResizableW){        
        this.updateWidth();
    }    
    this.cadre.style.overflow='auto';    
    this.divExt.style.overflow='auto';
}

rialto.widget.Frame.prototype.updateWidth=function(){  	
   if(this.open == true){
        if (this.autoResizableW){
	        //get new width of the parent
		    var tailleCalc=parseInt(this.getNewParentWidth());
		    this.divExt.style.width=tailleCalc-this.divExt.offsetLeft;
		    this.cadre.style.height=tailleCalc-this.divExt.offsetLeft-this.cadre.offsetLeft;
		    //call childs resize
		    this.resizeChilds(true,false);       
		}
	}
}

rialto.widget.Frame.prototype.updateHeight=function(){        
    if(this.open == true){
        if (this.autoResizableH){
	        //get new height of the parent
		    var tailleCalc=parseInt(this.getNewParentHeight());
		    this.divExt.style.height=tailleCalc-this.divExt.offsetTop;
		    this.cadre.style.height=tailleCalc-this.divExt.offsetTop-this.cadre.offsetTop;
	    	traceExec("Apres Calcul this.cadre.style.height:"+this.cadre.style.height+" this.divExt.style.height:"+this.divExt.style.height,1);
			//call childs resize
		    this.resizeChilds(false,true);       
		}
	}
    
}
/*
//Size update
rialto.widget.Frame.prototype.updateSize= function(delta){       
    if (this.autoResizableH || this.autoResizeContenu){            
        this.updateHeight(delta);        
    }
     //si auto Width
    if(this.autoResizableW){        
        this.updateWidth(delta);
    }    
}

rialto.widget.Frame.prototype.updateWidth=function(delta){
	this.resizeChilds(delta);
}

rialto.widget.Frame.prototype.updateHeight=function(delta) { 
    //fonction permettant de calculer les tailles des diff?rent ?l?ments        
    //AUTOSIZEPARENT        
    //alert("maj majTaille cadre " +delta);    
    if(this.open == true){
        if (this.autoResizableH){                       
            if (!delta){                    
                //calcule la nouvelle taille du parent
                //dans le cas du autoresizeParent on met une taille min de 100px
                var tailleCalc=Math.max(100,parseInt(this.getNewParentHeight())-this.divExt.offsetTop);
                //diff?rence avec la taille pr?c?dente
                var delta=tailleCalc-this.divExt.offsetHeight;
            }
            else{
                //calcule la nouvelle taille du parent
                //dans le cas du autoresizeParent on met une taille min de 100px
                var tailleCalc=Math.max(100,this.divExt.offsetHeight+delta);
                //si on d?passe en width on rajoute 18px pour l'ascensceur qui sera cr?e
                //A FAIRE......
            }   
            //alert("delta:"+delta+" tailleCalc:"+tailleCalc);
            if (delta<0){        
            //si on diminue la taille on maj les fils avant d'appliquer la modif
                //this.cadre.style.overflow='hidden';                
                this.resizeChilds(delta);
                //calcul de la taille du cadre avant d'appliquer la modif     
                if (this.dynamic == true) {                                    
                    this.cadre.style.height=tailleCalc-this.cadre.offsetTop+'px';                                                           
                }
                else{
                    this.cadre.style.height='100%';        
                }                
                //this.cadre.style.overflow='auto';
                //on diminue la div sup
                this.divExt.style.height=tailleCalc;
                //this.cadre.style.width='100%';
                //alert("DIMINUE CADRE this.cadre.style.height:"+this.cadre.style.height+" this.divExt.style.height:"+this.divExt.style.height);
            }
            else{
                //on augmente la div sup
                this.divExt.style.height=tailleCalc;                
                //calcul de la taille du cadre apres la modif                     
                if (this.dynamic == true) {                
                    this.cadre.style.height=tailleCalc-this.cadre.offsetTop+'px';                   
                }
                else{
                    this.cadre.style.height='100%';        
                }                
                //on agrandit les fils                                
                this.resizeChilds(delta);                
            }
            //declence le resize applicatif           
            this.resizeApplicatif(this.oldHeight,this.oldWidth,this.divExt.offsetHeight,this.divExt.offsetWidth);
            this.oldHeight=this.divExt.offsetHeight;
            this.oldWidth=this.divExt.offsetWidth;                            
        }            
    }    
}*/


rialto.widget.Frame.prototype.resizeContenu=function(){
//alert('Frame.resizeContenu'+this.id);
    //AUTOSIZECONTENU
    if (this.autoResizeContenu && this.autoResizeParent==false){               
        //Si le cadre n'est pas vide
        if (this.cadre.childNodes.length>0){
            //parcours les fils pour trouver la taille la plus basse (combinaison TOP+HEIGHT)
            taillelim=0;        
            for(var i=0;i<this.cadre.childNodes.length;i++){
                fils=this.cadre.childNodes[i];
                tailleLimFils=fils.offsetTop+fils.offsetHeight;               
                if (tailleLimFils>taillelim){taillelim=tailleLimFils;}        
            }            
            //on attribus cette taille si elle est inf?rieure ? la taille d?clar?e du cadre                                
            if (taillelim<parseInt(this.height)-this.cadre.offsetTop){            
                this.cadre.style.height=taillelim;
                //calcul de la taille du cadre    
                this.divExt.style.height=this.cadre.offsetHeight+this.cadre.offsetTop+'px';               
            }
            else{
                //on redonne les tailles initiales
                this.divExt.style.height=this.height;                        
                if (this.dynamic == true) {                
                    this.cadre.style.height=Math.max(0,this.divExt.offsetHeight-this.cadre.offsetTop)+'px';                   
                }
                else{
                    this.cadre.style.height='100%';        
                }                           
            }
            //on propage au pere
            this.resizePereFrere();            
        }            
    }        
}


rialto.widget.Frame.prototype.getContenuHeight=function(){
    //get the available heiht 
    return parseInt(this.cadre.style.height);
}

rialto.widget.Frame.prototype.setDisplay=function(open) {        
	//show the content   
    if (open) { 	  	                    
        //put the intit size  
        if (this.autoResizeParent){
            this.divExt.style.height=this.getNewParentHeight();
        }
        else{
            this.divExt.style.height=this.height; 
        }        
        this.cadre.style.display='block';        
        //autoresize
        if (this.autoResizeParent){
            this.updateSize();        
        }
        //if no autoresize call the child resize
        else{
            if (this.autoResizeContenu){this.resizeContenu();}
            this.resizeChilds();
        }                       
        this.BTN.setImageReference("lessButtonOff","lessButtonOn")
        this.BTN.alt = "Close the frame";     	    
    }
    else {            
    	//hide the content
        this.cadre.style.display='none';
        this.divExt.style.height=25;        
        this.BTN.setImageReference("moreButtonOff","moreButtonOn")
        this.BTN.alt = "Open the frame";
    } 
    this.open = open; 
	if(this.inContainer){
	    this.inContainer.resizeChilds(false,true); 
	}	
	//adapt component
	this.$$activeContent();
	//call the applicative method
    this.onSetDisplay(this.open);   
}

rialto.widget.Frame.prototype.setTitle=function(newTitle){
    //change the title
    this.labelTitle.setText(newTitle);
}

rialto.widget.Frame.prototype.onSetDisplay=function(open) {
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
//open is a boolean indicating the open state of the frame
}